import request from '@/api/request'

/**
 * 触发一次全局智能调度
 */
export function schedulePieces() {
    return request({
        url: '/dispatch/schedule',
        method: 'post'
    })
}

/**
 * 根据工位ID获取其实时队列
 * @param {string} wsid - 工位ID
 */
export function getWorkstationQueue(wsid) {
    return request({
        url: `/dispatch/queue/${wsid}`,
        method: 'get'
    })
}

/**
 * 启动工件加工
 * @param {number} dispatchId - 调度记录ID
 */
export function startProcessing(dispatchId) {
    return request({
        url: `/dispatch/${dispatchId}/start`,
        method: 'put'
    })
}

/**
 * 完成工件加工
 * @param {number} dispatchId - 调度记录ID
 */
export function completeProcessing(dispatchId) {
    return request({
        url: `/dispatch/${dispatchId}/complete`,
        method: 'put'
    })
}

/**
 * 根据工件ID获取其生命周期
 * @param {string} pieceId - 工件ID
 */
export function getPieceLifecycle(pieceId) {
    return request({
        url: `/dispatch/lifecycle/${pieceId}`,
        method: 'get'
    })
}

export function getActiveTasks() {
    return request({
        url: '/dispatch/active-tasks',
        method: 'get'
    })
}

export function pauseProcessing(dispatchId) {
    return request({
        url: `/dispatch/${dispatchId}/pause`,
        method: 'put'
    });
}

export function resumeProcessing(dispatchId) {
    return request({
        url: `/dispatch/${dispatchId}/resume`,
        method: 'put'
    });
}

export function reorderWorkstationQueue(data) {
    return request({
        url: '/dispatch/queue/reorder',
        method: 'put',
        data // data 是一个数组: [{ dispatchId, newQueueId }, ...]
    })
}
